//*********************************************************************
// Copyright (C) 2014 Hell Prototypes / www.hellprototypes.com
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
//********************************************************************
#include <avr/io.h>
#include <string.h>
#include "common.h"

static bool read(byte, byte*, byte);
static bool write(byte, byte);

void PCF8563_init()
{
	byte data;

	//FIXME: Move this work to loader
	i2c_start(0x30);
	i2c_write(0x20);
	i2c_write(0x02);
	i2c_stop();

	if(!read(PCF8563ADDR_CTRL_1, &data, 1)) {
		return;
	}
	if(data != 0x00) {
		if(!write(PCF8563ADDR_CTRL_1, 0x00)) {
			return;
		}
	}

	if(!read(PCF8563ADDR_CLKOUT_CTRL, &data, 1)) {
		return;
	}
	if(data & 0x80) {
		if(!write(PCF8563ADDR_CLKOUT_CTRL, 0x00)) {
			return;
		}
	}

	if(!read(PCF8563ADDR_TIMER_CTRL, &data, 1)) {
		return;
	}
	if((data & 0x83) != 0x03) {
		if(!write(PCF8563ADDR_TIMER_CTRL, 0x03)) {
			return;
		}
	}

	if(!read(PCF8563ADDR_SECS, &data, 1)) {
		return;
	}
	if(data & 0x80) {
		write(PCF8563ADDR_SECS, data & 0x7F);
	}

	//PORTA.PIN5CTRL	= 0x18;//PIN_RTC_ACC_INT - pull up, any edge
	PORTA.INT0MASK |= 0x20;//RTC_ACC_INT at PA5
}

// Get time
void PCF8563_get(s_time* timeData)
{
	read(PCF8563ADDR_SECS, (byte*)timeData, sizeof(s_time));

	//Mask unused bits
	//timeData->secs  = timeData->secs & 0x7f;
	timeData->mins  = timeData->mins & 0x7f;
	timeData->hours = timeData->hours& 0x3f;
	timeData->date  = timeData->date & 0x3f;
	timeData->day   = timeData->day  & 0x07;
	timeData->month = timeData->month& 0x1f;
	//timeData->year  = timeData->year;
}

// I2C read
static bool read(byte address, byte* data, byte count)
{
	// Start
	if(!i2c_start(PCF8563I2C_W)) {
		i2c_stop();
		return false;
	}

	if(!i2c_write(address)) { // Register address
		i2c_stop();
		return false;
	}

	// Repeat start
	if(!i2c_start(PCF8563I2C_R)) {
		i2c_stop();
		return false;
	}

	// Read data
	for(byte i=0;i<count;i++)
	{
		if(!i2c_read(&data[i], (i < (count - 1)) ? I2C_ACK : I2C_NACK)) {
			i2c_stop();
			return false;
		}
	}

	// Stop
	i2c_stop();

	return true;
}

// I2C write
static bool write(byte address, byte data)
{
	// Star/
	if(!i2c_start(PCF8563I2C_W)) {
		i2c_stop();
		return false;
	}

	if(!i2c_write(address)) { // Register address
		i2c_stop();
		return false;
	}

	if(!i2c_write(data)) { 
		i2c_stop();
		return false;
	}

	// Stop
	i2c_stop();

	return true;
}


